/**
 * 
 */
package action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import myfamily.model.Contact;
import myfamily.model.GenericPerson;
import myfamily.model.Name;
import myfamily.model.RegisteredUser;

import org.expressme.openid.Authentication;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author JATIN
 *
 */
public class AddChild extends ActionSupport{
	private List<String> operations;
	private List<String> familymembers;
	private List<String> spouseList;
	private String memberName;
	private String memberGender;
	private String memberEmailId;
	private String selectedOption;
	private String proceed;
	private String selectedSpouse;
	private String newSpouseName;
	private String newSpouseGender;
	private String newSpouseEmailId;
	private String authId;
	private String treeString;



	public String execute(){

		try{
			Map session = ActionContext.getContext().getSession();
			Authentication auth = new Authentication();
			auth = (Authentication)session.get("authentication");
			setAuthId(auth.getIdentity().split("=")[1]);
			RegisteredUser ru = RegisteredUser.login(getAuthId());
			GenericPerson gp = (GenericPerson)session.get("selectedPerson");
			if(proceed==null){

				ArrayList<GenericPerson> spouses = gp.getSpouseList();
				spouseList = new ArrayList<String>();
				for(GenericPerson g:spouses){
					spouseList.add(g.getPersonName().toString());
				}

				return "showAddChildForm";

			}else{

				if(selectedOption.equals("With Existing Spouse")){

					ArrayList<GenericPerson> spouses = gp.getSpouseList();

					for(GenericPerson g:spouses){
						if(g.getPersonName().toString().equals(selectedSpouse)){
							GenericPerson child;
							if(getMemberName().split(" ").length == 1)
								child = ru.createUnregisteredUser(new Name(getMemberName(), "", gp.getPersonName().getLastName()),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));
							else
								child = ru.createUnregisteredUser(new Name(getMemberName().split(" ")[0], "", getMemberName().split(" ")[1]),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));

							gp.addChild(g,child);
							break;
						}
					}



				}else{
					if(selectedOption.equals("With New Spouse")){
						GenericPerson spouse;
						if(getMemberName().split(" ").length == 1)
							spouse = ru.createUnregisteredUser(new Name(getNewSpouseName(), "", gp.getPersonName().getLastName()),getNewSpouseGender(),"","",null,null,new Contact("",getNewSpouseEmailId()));
						else
							spouse = ru.createUnregisteredUser(new Name(getNewSpouseName().split(" ")[0], "", getNewSpouseName().split(" ")[1]),getNewSpouseGender(),"","",null,null,new Contact("",getNewSpouseEmailId()));

						GenericPerson child;
						if(getMemberName().split(" ").length == 1)
							child = ru.createUnregisteredUser(new Name(getMemberName(), "", gp.getPersonName().getLastName()),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));
						else
							child = ru.createUnregisteredUser(new Name(getMemberName().split(" ")[0], "", getMemberName().split(" ")[1]),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));

						gp.addChild(spouse,child);	


					}
					else{

						GenericPerson d = ru.createDummyProfile();
						GenericPerson child;
						if(getMemberName().split(" ").length == 1)
							child = ru.createUnregisteredUser(new Name(getMemberName(), "", gp.getPersonName().getLastName()),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));
						else
							child = ru.createUnregisteredUser(new Name(getMemberName().split(" ")[0], "", getMemberName().split(" ")[1]),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));
						gp.addChild(d,child);	


					}
				}

				operations = new ArrayList<String>();
				operations.add("Add Child");
				operations.add("Add Parents");
				operations.add("Add Partner");
				operations.add("Add Sibling");
				operations.add("Delete Member");
				operations.add("Edit Details");
				familymembers = new ArrayList<String>();
				/* update the familymembers list*/
				familymembers.clear();
				ArrayList<GenericPerson> fm = ru.identifyExtremeAncestors();
				for(GenericPerson g: fm){
					familymembers.add(g.getPersonName().toString());

				}

				setTreeString(ru.printTree().toString());
				return "showModifiedTree";

			}




		}catch(Exception e){

		}

		return "showLogin";


	}



	public String getMemberName() {
		return memberName;
	}



	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}



	public String getMemberGender() {
		return memberGender;
	}



	public void setMemberGender(String memberGender) {
		this.memberGender = memberGender;
	}



	public String getMemberEmailId() {
		return memberEmailId;
	}



	public void setMemberEmailId(String memberEmailId) {
		this.memberEmailId = memberEmailId;
	}



	public String getSelectedOption() {
		return selectedOption;
	}



	public void setSelectedOption(String selectedOption) {
		this.selectedOption = selectedOption;
	}



	public String getProceed() {
		return proceed;
	}



	public void setProceed(String proceed) {
		this.proceed = proceed;
	}



	public String getSelectedSpouse() {
		return selectedSpouse;
	}



	public void setSelectedSpouse(String selectedSpouse) {
		this.selectedSpouse = selectedSpouse;
	}



	public String getNewSpouseName() {
		return newSpouseName;
	}



	public void setNewSpouseName(String newSpouseName) {
		this.newSpouseName = newSpouseName;
	}



	public String getNewSpouseGender() {
		return newSpouseGender;
	}



	public void setNewSpouseGender(String newSpouseGender) {
		this.newSpouseGender = newSpouseGender;
	}



	public String getNewSpouseEmailId() {
		return newSpouseEmailId;
	}



	public void setNewSpouseEmailId(String newSpouseEmailId) {
		this.newSpouseEmailId = newSpouseEmailId;
	}



	public String getAuthId() {
		return authId;
	}



	public void setAuthId(String authId) {
		this.authId = authId;
	}



	public List<String> getSpouseList() {
		return spouseList;
	}



	public void setSpouseList(List<String> spouseList) {
		this.spouseList = spouseList;
	}



	public String getTreeString() {
		return treeString;
	}



	public void setTreeString(String treeString) {
		this.treeString = treeString;
	}



	public List<String> getOperations() {
		return operations;
	}



	public void setOperations(List<String> operations) {
		this.operations = operations;
	}



	public List<String> getFamilymembers() {
		return familymembers;
	}



	public void setFamilymembers(List<String> familymembers) {
		this.familymembers = familymembers;
	}

}
